﻿/****************************************************************
*   作者：qq918
*   创建时间：2018/2/3 17:50:00
*   描述说明：
*****************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Model
{
  /// <summary>
  /// 物品管理器
  /// </summary>
  public class InventoryManager : MonoBehaviour
  {
    /// <summary>
    /// 所有物品的信息
    /// </summary>
    public Dictionary<int, ItemEntity> Dictionary = new Dictionary<int, ItemEntity>();

    /// <summary>
    /// 所有的物品信息
    /// </summary>
    [Header("物品")]
    public TextAsset MaterialTextAsset;
    public TextAsset WeaponTextAsset;
    public TextAsset EquipTextAsset;

    private void Awake()
    {
      UISlot.OnEnter += UISlot_OnEnter;
      UISlot.OnExit += UISlot_OnExit;
      UISlot.OnLeftBeginDrag += UISlot_OnLeftBeginDrag;
      UISlot.OnLeftEndDrag += UISlot_OnLeftEndDrag;
    }

    #region 事件处理
    /// <summary>
    /// 消息框
    /// </summary>
    public Tips tips;

    /// <summary>
    /// 是否在拖动
    /// </summary>
    private bool isDrag = false;

    /// <summary>
    /// 拖动的
    /// </summary>
    public UISlot DragItem;

    /// <summary>
    /// 
    /// </summary>
    public UIMouseFollow DragItem_UIMouseFollow;

    /// <summary>
    /// 高亮边框
    /// </summary>
    public Button heightlight;

    /// <summary>
    /// 结束拖动
    /// </summary>
    /// <param name="oldSlot">开始拖动时的</param>
    /// <param name="slot2">结束拖动时的</param>
    private void UISlot_OnLeftEndDrag(UISlot oldSlot, GameObject go)
    {
      isDrag = false;

      if (DragItem == null || DragItem.UIItem == null)
        return;

      DragItem.gameObject.SetActive(false);
      DragItem_UIMouseFollow.isFollow = false;
      DragItem.UIItem = null;

      if (go == null) // 表示不在UI上面，扔掉。
      {
        oldSlot.UIInventory.UpdateNumerText();
      }
      else
      {
        UISlot slot = go.GetComponent<UISlot>();
        if (slot != null && slot.Has(this.DragItem.VM))
        {
          // 交换，直接放。
          if (slot.UIItem == null)
          {
            //Debug.Log("直接放");
            slot.StoreItem(DragItem.VM);
          }
          else
          {
            //Debug.Log("交换");
            var item = slot.Replace(DragItem.VM);
            oldSlot.StoreItem(item);
          }
        }
        else
        {
          // 放回原位
          //Debug.Log("放回原位");
          oldSlot.StoreItem(DragItem.VM);
        }
      }
    }

    /// <summary>
    /// 开始拖动
    /// </summary>
    /// <param name="slot"></param>
    private void UISlot_OnLeftBeginDrag(UISlot slot)
    {
      // 空格子
      if (slot.UIItem == null) return;

      isDrag = true;
      tips.Hide();

      DragItem.Replace(slot.VM);
      slot.Dispose();

      DragItem.gameObject.SetActive(true);
      DragItem_UIMouseFollow.isFollow = true;
    }

    private void UISlot_OnExit(Transform obj)
    {
      tips.Hide();
    }

    private void UISlot_OnEnter(Transform sender, ItemEntity item)
    {
      if (!isDrag)
      {
        tips.Show(item);
      }
    }

    #endregion

    /// <summary>
    /// 加载物品
    /// </summary>
    public void LoadInventory()
    {
      List<Material> material = CSVHelper.FromCSV<Material>(MaterialTextAsset.text);
      ItemListToDic(material);

      List<Equipment> equip = CSVHelper.FromCSV<Equipment>(EquipTextAsset.text);
      ItemListToDic(equip);

      List<Weapon> weapon = CSVHelper.FromCSV<Weapon>(WeaponTextAsset.text);
      ItemListToDic(weapon);
    }

    /// <summary>
    /// 把物品添加进字典中
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    private void ItemListToDic<T>(List<T> list) where T : ItemEntity
    {
      foreach (var item in list)
      {
        Dictionary.Add(item.ID, item);
      }
    }
  }
}